#include "input.h"

Input::Input() {
    elements=0;
    dimension=0;
    sizeOfElements=0;
}

Input::~Input() {
    if(elements)
        delete[] elements;
}

const unsigned int& Input::getDimension() {
    return dimension;
}

void Input::createPrimeInput(const unsigned int& amountOfPrimes) {
    assert(amountOfPrimes);
    assert(elements==0);
    dimension=1;
    elements=new InputElement[amountOfPrimes];
    double* myPosition=new double[1];
    double value;
    myPosition[0]=1;
    value=2;
    elements[0].set(myPosition, value, dimension);

	unsigned int count=1;
	unsigned int primeSearchPosition=3;
	bool isPrime;
	while(count < amountOfPrimes) {
		isPrime=true;
		for(unsigned int i=3; ((i<(primeSearchPosition>>1)) && isPrime); i+=2) {
			if((primeSearchPosition % i)==0)
				isPrime=false;
		}
		if(isPrime) {
		    myPosition[0]=count+1;
		    value=primeSearchPosition;
			elements[count].set(myPosition, value, dimension);
			++count;
		}
		primeSearchPosition+=2;
	}
	sizeOfElements=amountOfPrimes;
	delete[] myPosition;
}

const unsigned int& Input::getSize() {
    return sizeOfElements;
}

InputIterator& Input::getNewIterator() {
    return *(new InputIterator(elements, sizeOfElements));
}

bool Input::importFromTextfile(const char* fileName) {
    assert(elements==0);
    assert(sizeOfElements==0);
	std::ifstream inFile;
	inFile.open(fileName);
	if (!inFile) {
	    std::cerr << "Unable to open input file " << fileName << "\n";
	    exit(1);   // call system to stop
	}

	std::string tmps, tmpsNumber;
	tmpsNumber="";
    std::string tmptmp=" ";
	if (getline(inFile, tmps)) {
        for (unsigned int i=0; i<tmps.size(); ++i) {
            if(tmps[i] < 48 || tmps[i] > 57)
                break;
            else {
                tmptmp[0]=tmps[i];
                tmpsNumber.append(tmptmp);
            }
        }
        dimension=atoi(tmpsNumber.c_str());
//        std::cout<<"input dimension = "<< dimension << std::endl;
    }
    else {
        std::cout<< "Invalid text input format. File contains no Data.\n";
        return false;
    }

    tmpsNumber="";
    LinkedList<InputElement> tmpList;
    double value;
    unsigned int lineCount=1;
    double* posArray = new double[dimension];
    unsigned int numberNr;
    bool inNumber;
	while (getline( inFile, tmps )) {
	    ++lineCount;
        numberNr=0;
        inNumber=false;
        tmps.append(" ");
        for (unsigned int i=0; i<tmps.size(); ++i) {
            if((tmps[i] < 48 || tmps[i] > 57) && tmps[i]!=46) {
                if(inNumber) {
                    if (numberNr==dimension) {
                        value=atof(tmpsNumber.c_str());
                        ++numberNr;
                    }
                    else {
                        posArray[numberNr++]=atof(tmpsNumber.c_str());
                    }
                }
                tmpsNumber="";
                inNumber=false;
            }
            else {
                tmptmp[0]=tmps[i];
                tmpsNumber.append(tmptmp);
                inNumber=true;
            }
        }
        if(dimension+1==numberNr) {
            tmpList.addAtEnd(*(new InputElement(posArray, value, dimension)));
		//std::cout<< "readed value: " << posArray << "  " << value;
        }
        else {
            std::cout<< "ERROR: couldn't read input file @ line nr. " << lineCount << std::endl;
        }
        assert(dimension+1==numberNr);
    }
    inFile.close();
    delete [] posArray;
    assert(elements==0);
    assert(tmpList.getSize()>0);
    elements = new InputElement[tmpList.getSize()];
    sizeOfElements = tmpList.getSize();
    tmpList.resetIteratorPosition();
    unsigned int i=0;
    while (tmpList.iteratorHasNext()) {
        elements[i++].set(tmpList.iteratorGetNext(), dimension);
    }
    return true;
}
